package com.example.dyy.servlet;

import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.example.dyy.entity.Movie;
import com.example.dyy.util.DBUtil;

@WebServlet("/movie/search")
public class MovieSearchServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) 
            throws ServletException, IOException {
        String keyword = request.getParameter("keyword");
        List<Movie> movies = new ArrayList<>();
        
        if (keyword != null && !keyword.trim().isEmpty()) {
            String sql = "SELECT * FROM movie WHERE name LIKE ? OR type LIKE ?";
            try (Connection conn = DBUtil.getConnection();
                 PreparedStatement pstmt = conn.prepareStatement(sql)) {
                
                String searchPattern = "%" + keyword.trim() + "%";
                pstmt.setString(1, searchPattern);
                pstmt.setString(2, searchPattern);
                
                ResultSet rs = pstmt.executeQuery();
                while (rs.next()) {
                    Movie movie = new Movie();
                    movie.setId(rs.getInt("id"));
                    movie.setName(rs.getString("name"));
                    movie.setDuration(rs.getInt("duration"));
                    movie.setType(rs.getString("type"));
                    movie.setDescription(rs.getString("description"));
                    movie.setPrice(rs.getDouble("price"));
                    movies.add(movie);
                }
            } catch (Exception e) {
                e.printStackTrace();
                throw new ServletException("搜索电影失败", e);
            }
        } else {
            // 如果没有搜索关键词，显示所有电影
            try (Connection conn = DBUtil.getConnection();
                 PreparedStatement pstmt = conn.prepareStatement("SELECT * FROM movie")) {
                
                ResultSet rs = pstmt.executeQuery();
                while (rs.next()) {
                    Movie movie = new Movie();
                    movie.setId(rs.getInt("id"));
                    movie.setName(rs.getString("name"));
                    movie.setDuration(rs.getInt("duration"));
                    movie.setType(rs.getString("type"));
                    movie.setDescription(rs.getString("description"));
                    movie.setPrice(rs.getDouble("price"));
                    movies.add(movie);
                }
            } catch (Exception e) {
                e.printStackTrace();
                throw new ServletException("获取电影列表失败", e);
            }
        }
        
        request.setAttribute("movies", movies);
        request.getRequestDispatcher("/movie/list.jsp").forward(request, response);
    }
} 